package com.suisung.shopsuite.pay;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IPNListenerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 读取IPN回调信息
        String callbackData = request.getReader().lines()
                .reduce("", (accumulator, actual) -> accumulator + actual);
        JSONObject data = JSONUtil.parseObj(callbackData);

        // 处理回调数据
        System.out.println("Received IPN Callback: " + callbackData);
        // 响应 "ok" 给Nihaopay服务
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("text/plain"); // 设置响应内容类型为文本
        response.getWriter().write("ok"); // 返回 "ok" 响应
    }
}